package team.seekers.skill.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.seekers.skill.common.utils.ParamCheckUtils;
import team.seekers.skill.entity.dao.Question;
import team.seekers.skill.entity.vo.admin.update.AdminQuestionAnswerVO;
import team.seekers.skill.result.Result;
import team.seekers.skill.result.ResultCode;
import team.seekers.skill.service.QuestionService;

@RestController
@RequestMapping("/api/admin/question")
public class AdminQuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 回答一个问题
     * @param adminQuestionAnswerVO
     * @return
     */
    @PostMapping("/answer")
    public Result answer(@RequestBody AdminQuestionAnswerVO adminQuestionAnswerVO){
        if(ParamCheckUtils.isNotRightId(adminQuestionAnswerVO.getQuestionId())){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        Question question = questionService.getById(adminQuestionAnswerVO.getQuestionId());
        if(question == null){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        question.setAnswer(adminQuestionAnswerVO.getAnswer());
        question.setIsAnswered(1);
        boolean updateById = questionService.updateById(question);
        if(!updateById){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }
}
